﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vit.Web.Models;
using Vit.Web.Providers.Data;

namespace Vit.AdminModule.Areas.Admin.Providers
{
    public class SqlCategoryPropertyDataProvider : DbContextDataProvider<WebProvidersDb, Vit.Web.Models.CategoryProperty>
    {
        protected override IQueryable<CategoryProperty> OrderByDefault(IQueryable<CategoryProperty> query)
        {
            return query.OrderBy(o => o.CategoryItemName).ThenBy(o => o.Order);
        }

        public override object[] GetKeys(CategoryProperty obj)
        {
            return new object[] { obj.CategoryItemName, obj.PropertyName };
        }

        public override CategoryProperty GetByKeys(params object[] ids)
        {
            if (ids == null || ids.Length != 2)
                return null;
            string key1 = ids[0].ToString();
            string key2 = ids[1].ToString();
            return db.CategoryProperties.FirstOrDefault(o => o.CategoryItemName == key1 && o.PropertyName == key2);
        }
    }
}